package com.zhu.esyblog.controller;


import com.zhu.esyblog.Utils.QIniuUtils;
import com.zhu.esyblog.pojo.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils;
import java.util.UUID;
@RestController
@RequestMapping("/upload")
@Api(tags = "图片上传模块")
public class UploadController {
    @Autowired
    private QIniuUtils qIniuUtils;
    private String url="re67i88y2.hd-bkt.clouddn.com";
    @PostMapping
    @ApiOperation(value = "图片上传")
    public RespBean upload(@RequestParam("image")MultipartFile file){
        //原始文件名称 aa.jpg
        String originalFilename = file.getOriginalFilename();
        //文件名称
        String filename=UUID.randomUUID().toString()+"."+ StringUtils.substringAfter(originalFilename,".");
       //上传到七牛云
        Boolean upload = qIniuUtils.upload(file, filename);
        if(upload){
            return RespBean.success("success",url+"/"+filename);
        }
        return RespBean.error("文件上传失败",20001);
    }

}
